package MyOs;
import java.util.*;
import queue.*;
public class corePart {
    private Algorithm algo=new AlgorithmSlice();
    private linkqueue lq=ProcessManager.getOperationQueue();
    private int len;
    private int[]digital;
    //private String[] string;
    private static int count=0;
    private int sign=-1;//标志位
    public corePart(){
       digital=new int[100];
       //string=new String[100];
    }
    public void showInfo(){
       len=lq.getSize();
       sign=-1;
       for(int i=0;i<len;i++){
           digital[i]=lq.getTraversePid();
          /* string[i]=lq.getTraverseStatus();*/
       }
        lq.backone();
        if(count<len){
        lq.choose(digital[count]).setRunning();
        int time=lq.choose(digital[count]).getRemainTime()-5;
        lq.choose(digital[count]).setRemainTime(time);
        if(count==0){
              int n=count;
              n++;
              while(n<len){
                 if(lq.choose(digital[n]).getStatus()==Status.RUNNING)
                     lq.choose(digital[n]).setReady();
                 n++;
              }
          }
          else if(count>0&&count<len-1){
              int m=count;
              int n=count;
              while(m<len-1){
                  m++;
                  if(lq.choose(digital[m]).getStatus()==Status.RUNNING){
                      lq.choose(digital[m]).setReady();
                  }
              }
              while(n>0){
                  n--;
                  if(lq.choose(digital[n]).getStatus()==Status.RUNNING)
                      lq.choose(digital[n]).setReady();
              }
          }
          if(count==len-1){
              int n=0;
              while(n<count){
                  if(lq.choose(digital[n]).getStatus()==Status.RUNNING)
                      lq.choose(digital[n]).setReady();
                  n++;
              }
          }
     }
        for(int i=0;i<len;i++){
            if(lq.choose(digital[i]).getRemainTime()<=0)
            System.out.println("pid:"+lq.choose(digital[i]).getPid()+" "+"name:"+lq.choose(digital[i]).getName()+" "+"remaintime:"+0+" "+"Status:"+lq.choose(digital[i]).getStatus());
            else
                System.out.println("pid:"+lq.choose(digital[i]).getPid()+" "+"name:"+lq.choose(digital[i]).getName()+" "+"remaintime:"+lq.choose(digital[i]).getRemainTime()+" "+"Status:"+lq.choose(digital[i]).getStatus());
        }
        System.out.println("-------------------------------------------------------------------");
        if(lq.choose(digital[count]).getRemainTime()<=0){
            lq.delete(digital[count]);
            sign=1;
        }
        if(sign==1){
            if(count!=len-1)
                count=count;
            else
                count=0;
        }
        if(sign==-1){
            if(count==len-1)
                count=0;
            else
                count++;
        }
      }
}
